import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/common_components/business_object_attribute.dart';
import 'package:x3_business_core/common_models/business_attribute_model.dart';

import '../page/business_clerk_manage_edit.dart';

class BusinessClerkFiles extends StatefulWidget {
  final String userId;

  const BusinessClerkFiles({Key key, @required this.userId}) : super(key: key);

  @override
  State<BusinessClerkFiles> createState() => _BusinessClerkFilesState();
}

class _BusinessClerkFilesState extends State<BusinessClerkFiles> with AutomaticKeepAliveClientMixin {
  final BaseFutureController futureController = BaseFutureController();

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BaseScreenRefreshComponent(
      controller: futureController,
      request: BaseScreenRequestModel('/x3-service-mobile-center/app/api/workbench/guide/detail/{guideId}', method: BaseHttpMethodEnum.GET, params: {'guideId': widget.userId}),
      builder: (BaseHttpResultModel result) {
        if (!(result.data is Map)) return BaseEmptyComponent();
        List<BusinessAttributeModel> attributeList = getAttributeList(result.data);
        return Stack(
          children: [
            BusinessObjectAttribute(attributeList: attributeList),
            Positioned(
              top: 24,
              right: 32,
              child: InkWell(
                onTap: () => onJumpCustomerDetailsEdit(),
                child: Row(
                  children: [
                    Text('完善资料', style: BaseStyles.subtitle2(context).copyWith(color: Color(0xFFCCCCCC))),
                    BaseComponentUtils.buildListViewRightIcon(context),
                  ],
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  List<BusinessAttributeModel> getAttributeList(Map<String, dynamic> data) {
    List<BusinessAttributeModel> attributeList = [];
    // 基本信息
    Map<String, dynamic> baseArchive = BaseUtils.getKeyData(data, 'baseArchiveResponse');
    attributeList.add(BusinessAttributeModel(title: '基本信息', itemList: getBaseArchiveList(baseArchive)));
    return attributeList;
  }

  /// 获取基本信息
  List<AttributeItemModel> getBaseArchiveList(Map<String, dynamic> baseArchive) {
    List<AttributeItemModel> list = [];
    // 用户编号
    String guideId = BaseUtils.getKeyData(baseArchive, 'guideId');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '用户编号', value: guideId), showCopied: true));
    // 用户名称
    String guideName = BaseUtils.getKeyData(baseArchive, 'guideName');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '用户姓名', value: guideName)));
    // 性别
    String sex = BaseUtils.getKeyData(baseArchive, 'sex');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '性别', value: sex)));
    // 工号
    String jobNumber = BaseUtils.getKeyData(baseArchive, 'jobNumber');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '工号', value: jobNumber), showCopied: true));

    // 入职日期
    String entryDate = BaseUtils.getKeyData(baseArchive, 'entryDate');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '入职日期', value: entryDate)));

    // 离职日期
    String leaveDate = BaseUtils.getKeyData(baseArchive, 'leaveDate');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '离职日期', value: leaveDate)));

    // 入职天数
    String entryDays = BaseUtils.getKeyData(baseArchive, 'entryDays');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '入职天数', value: entryDays)));

    // 所属店铺
    String departmentName = BaseUtils.getKeyData(baseArchive, 'departmentName');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '所属店铺', value: departmentName)));

    // 所属用户组
    String userGroupName = BaseUtils.getKeyData(baseArchive, 'userGroupName');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '所属用户组', value: userGroupName)));

    // 状态
    String status = BaseUtils.getKeyData(baseArchive, 'status');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '状态', value: status)));

    // 系统角色
    String roleName = BaseUtils.getKeyData(baseArchive, 'roleName');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '系统角色', value: roleName)));

    // 用户职位
    String userDuty = BaseUtils.getKeyData(baseArchive, 'userDuty');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '用户职务', value: userDuty)));

    // 身份证号
    String idCard = BaseUtils.getKeyData(baseArchive, 'idCard');
    list.add(AttributeItemModel(attribute: BaseKeyValueModel(key: '身份证号', value: idCard)));

    return list;
  }

  void onJumpCustomerDetailsEdit() {
    BaseRouterUtil.getInstance().pushNamed(BusinessClerkManageEdit.sName, arguments: {'userId': widget.userId}).then(onThen);
  }

  void onThen(value) {
    futureController.refresh();
  }
}
